#include <STC15F2K60S2.H>
/*#include <intrins.h>
#define somenop {_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); }
bit s_flag;
unsigned char i;
unsigned char inter;
unsigned int distance;
unsigned int time;
void Timer0Init(void);
void display();
void send_wave(void);
void pppp();
code unsigned char tab[] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
unsigned char dspbuf[8] = {10,10,10,10,10,10,10,10};  //显示缓冲区
sbit sen=P1^0;
sbit rec=P1^1;
void main()
{
  Timer0Init();
	pppp();
	while(1)
	{
		if(s_flag)
		{
			s_flag=0;
			 
      send_wave();
			TR1 = 1;
			while((rec==1)&&(TF1==0));//等待接收，且没有溢出出
			TR1 = 0;
			if(TF1==1)
			{
				TF1=0;
				distence=9999;
			}
			else
			{
				time=TH1;
				time<<=8;
				time|=TL1;
				distence=(unsigned int)(time*(0.017));
			}
			TH1=0;
			TL1=0;;
		}
		
			dspbuf[0]=distence/100;
			dspbuf[1]=distence/10%10;
			dspbuf[2]=distence%10;
	}
}

void Timer0Init(void)		
{
	TMOD |= 0x11;		
	TL0 = (65536-2000)%256;		
	TH0 = (65536-2000)/256;	
	TL1 = 0;
	TH1 = 0;
	TF0 = 0;
  ET0 = 1;
  EA	= 1;
	TR0 = 1;	
}

void timer_irs(void) interrupt 1
{
	TL0 = (65536-2000)%256;		
	TH0 = (65536-2000)/256;
	display();//2ms刷新一次
	if(++inter==200)
	{
		s_flag=1;
		inter=0;
	}
}

void display()
{
	
	//消影
	P2=((P2&0x1f)|0xef);
	P0=0xff;
	P2&=0x1f;
	
	//位选
	P2=((P2&0x1f)|0xcf);
	P0=1<<i;
	P2&=0x1f;
	
	//段选
	P2=((P2&0x1f)|0xef);
	P0=tab[dspbuf[i]];
	P2&=0x1f;
	
	if(++i==8)
	{
		i=0;
	}
}

void send_wave(void)
{
	unsigned char n=8;
	do
	{
	sen = 1;
	somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;
	somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;
	rec = 0;
	somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;
	somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;
	}while(n--);
}
*/


#include "intrins.h"
#include "absacc.h"

#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}

sbit TX = P1^0;  //发射引脚
sbit RX = P1^1;  //接收引脚

code unsigned char tab[] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
unsigned char dspbuf[8] = {10,10,10,10,10,10,10,10};  //显示缓冲区

bit s_flag;//更新标志位
unsigned char i;

void send_wave(void);
void display(void);



void main(void)
{
    unsigned int distance;
	unsigned int time = 0;
  TMOD |= 0x11;		
	TL0 = (65536-2000)%256;		
	TH0 = (65536-2000)/256;	
	TL1 = 0;
	TH1 = 0;
	TF0 = 0;
  ET0 = 1;
  EA	= 1;
	TR0 = 1;	 
   
	  
   while(1)
	{
		if(s_flag)
		{
			s_flag=0;
			 
      send_wave();
			TR1 = 1;
			while((RX==1)&&(TF1==0));//等待接收，且没有溢出出
			TR1 = 0;
			if(TF1==1)
			{
				TF1=0;
				distance=9999;
			}
			else
			{
				time=TH1;
				time<<=8;
				time|=TL1;
				distance=(unsigned int)(time*(0.017));
			}
			TH1=0;
			TL1=0;;
		}
		
			dspbuf[0]=distance/100;
			dspbuf[1]=distance/10%10;
			dspbuf[2]=distance%10;
	}
}

//定时器0中断服务函数
void timer_irs(void) interrupt 1
{
	unsigned char inter;
	TL0 = (65536-2000)%256;		
	TH0 = (65536-2000)/256;
	display();//2ms刷新一次
	if(++inter==200)
	{
		s_flag=1;
		inter=0;
	}
}


//显示函数  
void display()
{
	
	//消影
	P2=((P2&0x1f)|0xef);
	P0=0xff;
	P2&=0x1f;
	
	//位选
	P2=((P2&0x1f)|0xcf);
	P0=1<<i;
	P2&=0x1f;
	
	//段选
	P2=((P2&0x1f)|0xef);
	P0=tab[dspbuf[i]];
	P2&=0x1f;
	
	if(++i==8)
	{
		i=0;
	}
}


//TX引脚发送40KHz方波信号驱动超声波发送探头
//使用软件延时注意RC振荡器频率
void send_wave(void)
{
	unsigned char i=8;
	do
	{
	TX=1;
		somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;
		somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;
  TX=0;
		somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;
		somenop;somenop;somenop;somenop;somenop;somenop;somenop;somenop;
	}while(i--);
}

